package com.wondertek.poms.pre.manager.move;

import java.io.File;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.utils.FileIoUtiles;
import com.wondertek.poms.common.utils.StringUtil;
import com.wondertek.poms.core.service.ISystemConfigService;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.SyncTask;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * <p><b>MoveNasFileManager Description:</b> 从上游nas移动媒资文件到poms nas</p>
 * @author douzi
 * <b>DATE</b> 2020年12月22日 下午6:52:56
 */
@Slf4j
@Component
public class MoveNasFileManager {
    @Autowired
    private ISystemConfigService systemConfigService;
    @Autowired
    private AmqpTemplate amqpTemplate;
	
	/**
	 * <p><b>Title:</b> process</p>
	 * <p><b>Description:</b> 主方法</p>
	 * @author douzi
	 */
	public void process(SyncTask st, Content c) {
		// 1.发送mq消息去转图
		log.info("内容同步主流程|业务数据：" + c.getContentId() + "|媒资id：" + c.getAssetId() + "发送消息去转图");
		amqpTemplate.convertAndSend(Constants.QUEUE_TRANS_IMG, JSONUtil.toJsonStr(st));
		
		log.info("内容同步主流程|业务数据：" + c.getContentId() + "|媒资id：" + c.getAssetId() + "写poms存储路径开始");
        //清空或新建 poms的存储路径
        String pomsImageStorePath = systemConfigService.findAllBykey("poms_syncFile_basePath") + 
        		"/zhengshi" + contentIdToPath(StringUtil.null2Str(c.getContentId())) + "/display/";
        File pomsImagePath = new File(pomsImageStorePath);
        if (!pomsImagePath.exists()) {
            pomsImagePath.mkdirs();
        } else {
            FileIoUtiles.deleteFile(pomsImagePath);
        }

        //从同步基本路径copy图片进poms存储路径
        String origImagePath = systemConfigService.findAllBykey("syncFile_basePath") + 
        		"/zhengshi" + contentIdToPath(StringUtil.null2Str(c.getContentId())) + "/display/";
        File imageFile = new File(origImagePath);
        if (imageFile.exists() && imageFile.isDirectory()) {
            File[] imgs = imageFile.listFiles();
            if (imgs != null && imgs.length > 0) {
                for (File img : imgs) {
                    FileIoUtiles.fileCopy(img.getAbsolutePath(), pomsImageStorePath + img.getName(), true);
                }
            }
        }

        //拷贝节目单 playbill
        String playBillToPoms = systemConfigService.findAllBykey("poms_syncFile_basePath") + 
        		"/zhengshi" + contentIdToPath(StringUtil.null2Str(c.getContentId())) + "/program/playbill.xml";
        String playBillPath = systemConfigService.findAllBykey("syncFile_basePath") + "/" + c.getPlaybillFilePath();
        File file = new File(playBillPath);
        if (file.exists()) {
            FileIoUtiles.fileCopy(playBillPath, playBillToPoms, true);
        }

        //拷贝Metadata_M.xml
        String metadataPoms = systemConfigService.findAllBykey("poms_syncFile_basePath") + 
        		"/zhengshi" + contentIdToPath(StringUtil.null2Str(c.getContentId()));
        String path = systemConfigService.findAllBykey("syncFile_basePath") + 
        		"/zhengshi" + contentIdToPath(StringUtil.null2Str(c.getContentId()));
        String filename = systemConfigService.findAllBykey("metadataFilename").toString();
        File origMetadata = new File(path, filename);
        if (origMetadata.exists()) {
            FileIoUtiles.fileCopy(path + filename, metadataPoms + filename, true);
        }
        log.info("内容同步主流程|业务数据：" + c.getContentId() + "|媒资id：" + c.getAssetId() + 
        		"写poms存储路径结束|" + pomsImageStorePath + "|" + origImagePath + "|" + playBillToPoms);
	}
	
	/**
	 * <p><b>Title:</b> contentIdToPath</p>
	 * <p><b>Description:</b> 内容id，转换成路径</p>
	 * @author douzi
	 * @param contentId
	 * @return
	 */
	public static String contentIdToPath(String contentId) {
        return "/" + contentId.substring(0, 4) + "/" + contentId.substring(4, 7) + 
        		"/" + contentId.substring(7, 10) + "/" + contentId + "/";
    }
	
}
